//
//  SelectDatePopView.swift
//  Employee
//
//  Created by swordjoy on 2017/3/31.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit

class SelectDatePopView: UIView {

  var selectDate: ( (String) -> () )?
  
  var cancleClosure: VoidClosure?
  
  var leftTitle: String = "" {
    didSet {
      cancleButton.setTitle(leftTitle, for: .normal)
    }
  }
  
  var datePickerMode: UIDatePickerMode = .date {
    didSet {
      datePicker.datePickerMode = datePickerMode
    }
  }
  
  fileprivate var backgroundV: UIView!
  fileprivate var contentV: UIView!
  
  fileprivate var titleLabel: UILabel!
  fileprivate var cancleButton: UIButton!
  fileprivate var okButton: UIButton!
  fileprivate var datePicker: UIDatePicker!

  fileprivate var fromFrame: CGRect!
  fileprivate var endFrame: CGRect!
  
  override init(frame: CGRect) {
    super.init(frame: frame)
    
    backgroundColor = UIColor.clear
    
    fromFrame = CGRect(x: 0, y: UIScreen.height + 260, width: UIScreen.width, height: 260)
    endFrame = CGRect(x: 0, y: UIScreen.height - 260, width: UIScreen.width, height: 260)
    
    create()
    gesture()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

}

// MARK: - interface

extension SelectDatePopView {
  
  func show(in view: UIView){
    
    view.addSubview(self)
    UIView.animate(withDuration: 0.48) {
      self.contentV.frame = self.endFrame
      self.backgroundV.alpha = 1
    }
    
  }
  
}

// MARK: - action

extension SelectDatePopView {
  
  @objc fileprivate func cancleAction() {
    
    dismiss()
    
    guard let closure = cancleClosure else { return }
    closure()
    
  }
  
  @objc fileprivate func okAction() {
    
    guard let closure = selectDate else { return }
    
    let dateString = datePicker.date.inLocalRegion().string(custom: "yyyy-MM-dd")
    closure(dateString)
    
    dismiss()
    
  }
  
}

// MARK: - fileprivate

extension SelectDatePopView {
  
  @objc fileprivate func dismiss() {
    UIView.animate(withDuration: 0.48, animations: {
      self.contentV.frame = self.fromFrame
      self.backgroundV.alpha = 0
    }) { (true) in
      self.removeFromSuperview()
    }
  }
  
  fileprivate func gesture() {
    
    let tap = UITapGestureRecognizer(target: self, action: #selector(dismiss))
    backgroundV.addGestureRecognizer(tap)
    
  }
  
  fileprivate func create() {
    
    backgroundV = UIView(frame: UIScreen.main.bounds)
    addSubview(backgroundV)
    backgroundV.backgroundColor = GlobalConfig.BGLightGreyColor
    backgroundV.alpha = 0
    
    contentV = UIView(frame: fromFrame)
    contentV.backgroundColor = UIColor.white
    addSubview(contentV)
    
    titleLabel = UILabel()
    contentV.addSubview(titleLabel)
    titleLabel.textColor = GlobalConfig.TextDeepColor
    titleLabel.font = GlobalConfig.system15Font
    titleLabel.snp.makeConstraints { (make) in
      make.centerX.equalToSuperview()
      make.top.equalTo(20)
    }
    
    
    cancleButton = UIButton()
    contentV.addSubview(cancleButton)
    cancleButton.setTitle("取消", for: .normal)
    cancleButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    cancleButton.titleLabel?.font = GlobalConfig.system15Font
    cancleButton.addTarget(self, action: #selector(cancleAction), for: .touchUpInside)
    cancleButton.snp.makeConstraints { (make) in
      make.left.equalTo(10)
      make.top.equalTo(20)
      make.height.equalTo(15)
      make.width.equalTo(40)
    }
    
    okButton = UIButton()
    contentV.addSubview(okButton)
    okButton.setTitle("确定", for: .normal)
    okButton.setTitleColor(GlobalConfig.BGSkyBlueColor, for: .normal)
    okButton.titleLabel?.font = GlobalConfig.system15Font
    okButton.addTarget(self, action: #selector(okAction), for: .touchUpInside)
    okButton.snp.makeConstraints { (make) in
      make.right.equalTo(-10)
      make.top.equalTo(20)
      make.height.equalTo(15)
      make.width.equalTo(40)
    }
    
    datePicker = UIDatePicker()
    contentV.addSubview(datePicker)
    datePicker.datePickerMode = .date
    datePicker.snp.makeConstraints { (make) in
      make.left.equalTo(20)
      make.right.equalTo(-20)
      make.bottom.equalTo(-30)
      make.top.equalTo(titleLabel.snp.bottom).offset(50)
    }
    
  }
}
